{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark import SparkContext\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc = SparkContext()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "host = 'localhost'\n",
    "table = 'student'\n",
    "conf = {\"hbase.zookeeper.quorum\": host, \"hbase.mapreduce.inputtable\": table}\n",
    "keyConv = \"org.apache.spark.examples.pythonconverters.ImmutableBytesWritableToStringConverter\"\n",
    "valueConv = \"org.apache.spark.examples.pythonconverters.HBaseResultToStringConverter\"\n",
    "hbase_rdd = sc.newAPIHadoopRDD(\"org.apache.hadoop.hbase.mapreduce.TableInputFormat\",\n",
    "                               \"org.apache.hadoop.hbase.io.ImmutableBytesWritable\",\n",
    "                               \"org.apache.hadoop.hbase.client.Result\",\n",
    "                               keyConverter=keyConv,\n",
    "                               valueConverter=valueConv,\n",
    "                               conf=conf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MapPartitionsRDD[66] at mapPartitions at SerDeUtil.scala:244"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count = hbase_rdd.count()\n",
    "hbase_rdd.cache()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 {\"qualifier\" : \"age\", \"timestamp\" : \"1563325525969\", \"columnFamily\" : \"info\", \"row\" : \"1\", \"type\" : \"Put\", \"value\" : \"23\"}\n",
      "{\"qualifier\" : \"gender\", \"timestamp\" : \"1563325507162\", \"columnFamily\" : \"info\", \"row\" : \"1\", \"type\" : \"Put\", \"value\" : \"F\"}\n",
      "{\"qualifier\" : \"name\", \"timestamp\" : \"1563325498971\", \"columnFamily\" : \"info\", \"row\" : \"1\", \"type\" : \"Put\", \"value\" : \"Xueqian\"}\n",
      "2 {\"qualifier\" : \"age\", \"timestamp\" : \"1563325550387\", \"columnFamily\" : \"info\", \"row\" : \"2\", \"type\" : \"Put\", \"value\" : \"24\"}\n",
      "{\"qualifier\" : \"gender\", \"timestamp\" : \"1563325543423\", \"columnFamily\" : \"info\", \"row\" : \"2\", \"type\" : \"Put\", \"value\" : \"M\"}\n",
      "{\"qualifier\" : \"name\", \"timestamp\" : \"1563325536262\", \"columnFamily\" : \"info\", \"row\" : \"2\", \"type\" : \"Put\", \"value\" : \"Weiliang\"}\n"
     ]
    }
   ],
   "source": [
    "output = hbase_rdd.collect()\n",
    "for (k, v) in output:\n",
    "    print(k, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
